/*
 * KDZZgame
 *
 * @(#)XMLUtils.java      2009-6-25
 * 
 * Copyright 2010 RedPixy, Inc. All rights reserved.
 * Use is subject to license terms. Visit http://www.redpixy.com/
 * 
 */

package com.redpixy.monit.common.util;


import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author ZhangLei.
 * @version 2009-6-25
 * 
 * @category XML工具
 *
 */
public class XMLUtil {
	public static final Logger log = LoggerFactory.getLogger(XMLUtil.class);
	/**
	 * 得到class路径
	 * @return
	 */
	 public static String getClassParh(){
		 ClassLoader cl = Thread.currentThread().getContextClassLoader();
		 URL url = cl.getResource("//");
		 return  url.getPath().replace("%20", " ");
	 }
	 /**
	  * 将字符串转化为XML Document
	  * @param str
	  * @return
	  * @throws DocumentException
	  */
	 public static Document StringToXML(String str) throws DocumentException
	 {
	  Document document = DocumentHelper.parseText(str);
	  return document;
	 }
	 /**
	  * 保存XML 文件到指定目录
	  * @param document
	  * @param path
	  * @param name
	  * @throws IOException
	  */
	 public static void XMLWrite(Document document,String path ,String name) throws IOException
	 {
	  String savePath = path + name;
	  XMLWriter writer = new XMLWriter(new FileWriter(savePath));
	  writer.write(document);
	  writer.close();
	 }
	 /**
	  * 读取指定XML文件
	  * @param 
	  * @param path
	  * @param name
	  * @throws IOException
	  */
	 public static Document getXMLDocument(String fileName) {
		try {
			SAXReader saxread = new SAXReader();
			ClassLoader cl = Thread.currentThread().getContextClassLoader();
			URL url1 = cl.getResource(fileName);
			return saxread.read(url1).getDocument();
		} catch (DocumentException e) {
			log.error("",e);
			return null;
		}
	}
	
	 public static void main(String sa[]){
	 
		
		 
		 
	}
}
